#include <stdio.h>
#include <string.h>
int main()
{
    int k, i, j ,f;
    char c[10];
    int m[5][5];
    scanf("%d",&k);
    while(k--)
    {
        int flag[5][5]={0};
        int sign[5][5]={0};
        for(i = 0;i < 4;i++)
        {

            for(j = 0;j<4;j++)
            {
                scanf("%d",&m[i][j]);
                if(m[i][j]) flag[i][j]=1;    
            } 
        }
        scanf("%s",c);
        if(strcmp(c,"LEFT")==0)
        {
            for(i=0;i<4;i++)
            {
                for(j = 1;j<4;j++)
                {
                    f = j;
                    while(f!=0&&flag[i][f])
                    {
                        
                        if(!flag[i][f-1])
                        {
                            m[i][f-1] = m[i][f];
                            flag[i][f] = m[i][f] = 0;
                            flag[i][f-1] = 1;
                            f--;
                        }
                        else if(m[i][f-1]==m[i][f]&&!sign[i][f-1])
                        {
                            m[i][f-1] = m[i][f-1]*2;
                            flag[i][f] = m[i][f] = 0;
                            sign[i][f-1] = 1;
                            break;
                        }
                        else if(m[i][f-1]!= m[i][f]) break; 
                        else break;    
                    } 
                }
            }
        }
        else if(strcmp(c,"RIGHT")==0)
        {
            for(i=0;i<4;i++)
            {
                
                for(j = 2;j>=0;j--)
                {
                    f = j;
                    while(f!=3&&flag[i][f])
                    {
                        
                        if(!flag[i][f+1])
                        {
                            m[i][f+1] = m[i][f];
                            flag[i][f] = m[i][f] = 0;
                            flag[i][f+1] = 1;
                            f++;
                        }
                        else if(m[i][f+1]==m[i][f]&&!sign[i][f+1])
                        {
                            m[i][f+1] = m[i][f+1]*2;
                            flag[i][f] = m[i][f] = 0;
                            sign[i][f+1] = 1;
                            break;
                            
                        }
                        else if(m[i][f+1]!= m[i][f]) break;
                        else break;     
                    } 
                }
            }
        }
        else if(strcmp(c,"UP")==0)
        {
            for(i=0;i<4;i++)
            {
                for(j = 1;j<4;j++)
                {
                    f = j;
                    while(f!=0&&flag[f][i])
                    {
                        
                        if(!flag[f-1][i])
                        {
                            m[f-1][i] = m[f][i];
                            flag[f][i] = m[f][i] = 0;
                            flag[f-1][i] = 1;
                            f--;
                        }
                        else if(m[f-1][i]==m[f][i]&&!sign[f-1][i])
                        {
                            m[f-1][i] = m[f-1][i]*2;
                            flag[f][i] = m[f][i] = 0;
                            sign[f-1][i] = 1;
                            break;
                        }
                        else if(m[f-1][i]!= m[f][i]) break; 
                        else break;    
                    } 
                }
            }
        }
        else if(strcmp(c,"DOWN")==0)
        {
            for(i=0;i<4;i++)
            {
                for(j = 2;j>=0;j--)
                {
                    f = j;
                    while(f!=3&&flag[f][i])
                    {
                        
                        if(!flag[f+1][i])
                        {
                            m[f+1][i] = m[f][i];
                            flag[f][i] = m[f][i] = 0;
                            flag[f+1][i] = 1;
                            f++;
                        }
                        else if(m[f+1][i]==m[f][i]&&!sign[f+1][i])
                        {
                            m[f+1][i] = m[f+1][i]*2;
                            flag[f][i] = m[f][i] = 0;
                            sign[f+1][i] = 1;
                            break;
                        }
                        else if(m[f+1][i]!= m[f][i]) break; 
                        else break;    
                    } 
                }
            }
        }
        
        for(i = 0;i<4;i++) printf("%d %d %d %d\n",m[i][0],m[i][1],m[i][2],m[i][3]);
        printf("\n");
    }
}
